package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.BaoDanTiaoBean;
import com.oscar.ehome.bean.pagebean.BelongsBank;
import com.oscar.ehome.bean.pagebean.Dict;
import com.oscar.ehome.bean.pagebean.InsuranceCompany;
import com.oscar.ehome.bean.pagebean.ShuJuHuixianBean;
import com.oscar.ehome.bean.pagebean.SubBaoDanShuJuBEan;
import com.oscar.ehome.bean.pagebean.XuanXiangShuJuBean;
import com.oscar.ehome.bean.pagebean.policySheetTypeList;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.dateview.DateDialog;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.view.baodanshujuView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: SunHaoZhao
 * date: On 2018/4/24
 */

public class BaoDanNew_Activity extends BasesActivity<baodanshujuView, PagePresenter> implements baodanshujuView, View.OnClickListener {
    PagePresenter pagePresenter;
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.titlebar_img_right)
    ImageView titlebarImgRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.right_layout)
    RelativeLayout rightLayout;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.tianjiacheliang)
    EditText tianjiacheliang;
    @BindView(R.id.chexianshengxiaoshijian)
    TextView chexianshengxiaoshijian;
    @BindView(R.id.baoxiangongsi_chexian)
    TextView baoxiangongsiChexian;
    @BindView(R.id.baoandianhuachexian)
    TextView baoandianhuachexian;
    @BindView(R.id.chexianmokuai)
    LinearLayout chexianmokuai;
    @BindView(R.id.beibaoxianname)
    EditText beibaoxianname;
    @BindView(R.id.toubaorenname)
    EditText toubaorenname;
    @BindView(R.id.baoxiangongsi_shouxian)
    TextView baoxiangongsiShouxian;
    @BindView(R.id.baodanshengxiaoshijian)
    TextView baodanshengxiaoshijian;
    @BindView(R.id.jiaofeikasuoshuyinhang)
    TextView jiaofeikasuoshuyinhang;
    @BindView(R.id.yinhangkahao)
    EditText yinhangkahao;
    @BindView(R.id.baoandianhua)
    TextView baoandianhua;
    @BindView(R.id.dianzibaodan)
    TextView dianzibaodan;
    @BindView(R.id.chanpin_lin)
    LinearLayout chanpinLin;
    @BindView(R.id.shouxianmokuai)
    LinearLayout shouxianmokuai;
    @BindView(R.id.quesiton_tj)
    Button quesitonTj;
    String token;
    String[] shifou;
    String[] dianzibaodanshuzu;
    @BindView(R.id.biaotitupian)
    ImageView biaotitupian;
    @BindView(R.id.biaotiname)
    TextView biaotiname;
    List<XuanXiangShuJuBean.xuanxiang> baoxiangongsilist = new ArrayList<>();
    List<XuanXiangShuJuBean.xuanxiang> baoxiangongsichexianlist = new ArrayList<>();
    List<XuanXiangShuJuBean.xuanxiang> baozhangleixinglist = new ArrayList<>();
    List<XuanXiangShuJuBean.xuanxiang> suoshuyinhanglist = new ArrayList<>();
    List<XuanXiangShuJuBean.xuanxiang> jiatingshourulist = new ArrayList<>();
    String[] songsi;
    String[] baoxiangongsichexian;
    String[] baozhangleixingshuzu;
    String[] suoshuyinhangshuzu;
    String[] jiatingshourushuzu;
    String[] jiatingshourushuzuid;
    String[] jiatingshourushuzuvalve;
    String[] baoandianhuachexianshuzu;
    String[] baoandianhuashuzu;
    String[] jiaofeiqijianshuzu = new String[31];
    String[] baoxianqijianshuzu = new String[2];
    String jiatingid = "";
    String typee;
    String baoxiangongsiqita;
    String baoandianhuaqita;
    int baoanxiabiao;
    String baodan, id, baodanleixing, xiugaiid = "", bianji;
    List<String> beibaoxianrenjihe = new ArrayList<>();
    List<SubBaoDanShuJuBEan> baodanshuju = new ArrayList<>();
    Gson gson = new Gson();
    int baodanshuliang;
    Boolean faqi = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baodantuoguan_activity);
        ButterKnife.bind(this);
        initView();
        //默认添加一个Item

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tianjiaimg://点击添加按钮就动态添加Item
                if (!beibaoxianname.getText().toString().equals("")
                        && !toubaorenname.getText().toString().equals("")
                        && !baoxiangongsiShouxian.getText().toString().equals("")
                        && !baodanshengxiaoshijian.getText().toString().equals("")
                        && !jiaofeikasuoshuyinhang.getText().toString().equals("")
                        && !baoandianhua.getText().toString().equals("")
                        && !dianzibaodan.getText().toString().equals("")) {
                    for (int i = 0; i < chanpinLin.getChildCount(); i++) {
                        View childAt = chanpinLin.getChildAt(i);

                        EditText chanpinname = childAt.findViewById(R.id.chanpinname);
                        EditText nianjiaobaofei = childAt.findViewById(R.id.nianjiaobaofei);
                        EditText baoe = childAt.findViewById(R.id.baoe);
                        TextView jiaofeinianxian = childAt.findViewById(R.id.jiaofeinianxian);
                        TextView baoxianqijian = childAt.findViewById(R.id.baoxianqijian);
                        TextView baoxiangleixingmorenwenzi = childAt.findViewById(R.id.baoxiangleixingmorenwenzi);


                        if (!chanpinname.getText().toString().equals("")
                                && !nianjiaobaofei.getText().toString().equals("")
                                && !baoe.getText().toString().equals("")
                                && !jiaofeinianxian.getText().toString().equals("")
                                && !baoxianqijian.getText().toString().equals("")
                                && !baoxiangleixingmorenwenzi.getText().toString().equals("")

                                ) {
                            if (i == chanpinLin.getChildCount() - 1) {
                                addshouxianchanpin();
                                return;
                            }
                        } else {
                            ToastUtil.show(BaoDanNew_Activity.this, "您第" + (i + 1) + "组产品没有填写完整，请完善");
                        }

                    }
                } else {
                    ToastUtil.show(BaoDanNew_Activity.this, "您的基本信息没有填写完整，请完善");
                }


                break;
            case R.id.baoxiangongsi_shouxian:
                tankuangfanhui(songsi, baoxiangongsiShouxian, baoandianhua);
                break;
            case R.id.baoxiangongsi_chexian:
                tankuangfanhui(baoxiangongsichexian, baoxiangongsiChexian, baoandianhuachexian);
                break;
            case R.id.baodanshengxiaoshijian:
                getDate(baodanshengxiaoshijian);
                break;
            case R.id.chexianshengxiaoshijian:
                getDate(chexianshengxiaoshijian);
                break;
            case R.id.dianzibaodan:
                tankuangfanhui(dianzibaodanshuzu, dianzibaodan);
                break;
            case R.id.jiaofeikasuoshuyinhang:
                tankuangfanhui(suoshuyinhangshuzu, jiaofeikasuoshuyinhang, baoandianhua);
                break;
            case R.id.title_img_back:
                finish();
                break;
            case R.id.quesiton_tj:
                tankuangfanhui(shifou);
                //Subshuju();
                break;
            case R.id.home_tv_num:
                faqituoguan();

                break;
        }

    }

    public void faqituoguan() {
        CommonUtils.url_code = 0;
        //判断为空
        if (baodan.equals("1")) {

            if (!beibaoxianname.getText().toString().equals("")
                    && !toubaorenname.getText().toString().equals("")
                    && !baoxiangongsiShouxian.getText().toString().equals("")
                    && !baodanshengxiaoshijian.getText().toString().equals("")
                    && !jiaofeikasuoshuyinhang.getText().toString().equals("")
                    && !baoandianhua.getText().toString().equals("")
                    && !dianzibaodan.getText().toString().equals("")) {
                SubBaoDanShuJuBEan p = new SubBaoDanShuJuBEan();
                BelongsBank yinhang = new BelongsBank();

                InsuranceCompany sss = new InsuranceCompany();
                sss.setLabel(baoxiangongsiShouxian.getText().toString());
                sss.setValue(baoandianhua.getText().toString());
                for (int j = 0; j < baoxiangongsilist.size(); j++) {
//
                    if (baoxiangongsilist.get(j).getLabel().equals(baoxiangongsiShouxian.getText().toString())) {
                        sss.setId(baoxiangongsilist.get(j).getId());

                    }
                }
                p.setInsuranceCompany(sss);
                p.setInsuredName(beibaoxianname.getText().toString());
                p.setPolicyholderName(toubaorenname.getText().toString());


//


                List<policySheetTypeList> baozhang = new ArrayList<>();
                for (int i = 0; i < chanpinLin.getChildCount(); i++) {
                    View childAt = chanpinLin.getChildAt(i);

                    EditText chanpinname = childAt.findViewById(R.id.chanpinname);
                    EditText nianjiaobaofei = childAt.findViewById(R.id.nianjiaobaofei);
                    EditText baoe = childAt.findViewById(R.id.baoe);
                    TextView jiaofeinianxian = childAt.findViewById(R.id.jiaofeinianxian);
                    TextView baoxianqijian = childAt.findViewById(R.id.baoxianqijian);
                    TextView baoxiangleixingmorenwenzi = childAt.findViewById(R.id.baoxiangleixingmorenwenzi);


                    if (!chanpinname.getText().toString().equals("")
                            && !nianjiaobaofei.getText().toString().equals("")
                            && !baoe.getText().toString().equals("")
                            && !jiaofeinianxian.getText().toString().equals("")
                            && !baoxianqijian.getText().toString().equals("")
                            && !baoxiangleixingmorenwenzi.getText().toString().equals("")

                            ) {
                        policySheetTypeList baozhangleixingjiabaoe = new policySheetTypeList();
                        Dict dictduixiang = new Dict();
                        for(int j=0;j<baozhangleixinglist.size();j++){
                            if(baozhangleixinglist.get(j).getLabel().equals(baoxiangleixingmorenwenzi.getText().toString())){
                                dictduixiang.setId(baozhangleixinglist.get(j).getId());
                                dictduixiang.setLabel(baozhangleixinglist.get(j).getLabel());
                            }
                        }


                        baozhangleixingjiabaoe.setProtectionType(dictduixiang);
                        baozhangleixingjiabaoe.setInsuredAmount(baoe.getText().toString());
                        baozhangleixingjiabaoe.setPaymentPeriod(jiaofeinianxian.getText().toString());
                        baozhangleixingjiabaoe.setAnnualPremium(nianjiaobaofei.getText().toString());
                        baozhangleixingjiabaoe.setProductName(chanpinname.getText().toString());
                        baozhangleixingjiabaoe.setInsurePeriod(baoxianqijian.getText().toString());
                        baozhang.add(baozhangleixingjiabaoe);


                        if (i == chanpinLin.getChildCount() - 1) {
                            beibaoxianrenjihe.add(beibaoxianname.getText().toString());
                        }
                    } else {
                        if (baodanshuliang > 0) {
                            baodantuoguantankuang();
                        } else {
                            ToastUtil.show(BaoDanNew_Activity.this, "您第" + (i + 1) + "组产品没有填写完整，请完善");
                        }

                    }

                }
                p.setPolicyEffectiveTime(baodanshengxiaoshijian.getText().toString());
                p.setpolicySheetTypeList(baozhang);
                yinhang.setLabel(jiaofeikasuoshuyinhang.getText().toString());
                for (int j = 0; j < suoshuyinhanglist.size(); j++) {
                    if (suoshuyinhanglist.get(j).getLabel().equals(jiaofeikasuoshuyinhang.getText().toString())) {
                        yinhang.setId(suoshuyinhanglist.get(j).getId());

                    }
                }
                p.setInsuranceType("1");
                p.setBelongsBank(yinhang);
                if (!yinhangkahao.getText().toString().equals("")) {
                    p.setBankNo(yinhangkahao.getText().toString());
                }
                p.setReportPhone(baoandianhua.getText().toString());
                if (dianzibaodan.getText().toString().equals("是")) {
                    p.setIsElectronic("1");
                } else {
                    p.setIsElectronic("0");
                }
                p.setId(xiugaiid);
                String str = gson.toJson(p);
                Log.e("baodanshuju", "SubmitFeedBack: " + str);
                faqi = true;
                pagePresenter.subbaodanshuju(token, str, id, xiugaiid);
            } else {
                if (baodanshuliang > 0) {
                    baodantuoguantankuang();
                } else {
                    ToastUtil.show(BaoDanNew_Activity.this, "您的基本信息没有填写完整，请完善");
                }


            }
        } else {
            if (!tianjiacheliang.getText().toString().equals("") && !chexianshengxiaoshijian.getText().toString().equals("")
                    && !baoxiangongsiChexian.getText().toString().equals("")
                    && !baoandianhuachexian.getText().toString().equals("")) {
                SubBaoDanShuJuBEan p = new SubBaoDanShuJuBEan();
//

                p.setNumberPlate(tianjiacheliang.getText().toString());

                p.setPolicyEffectiveTime(chexianshengxiaoshijian.getText().toString());
                InsuranceCompany sss = new InsuranceCompany();
                sss.setLabel(baoxiangongsiChexian.getText().toString());
                sss.setValue(baoandianhuachexian.getText().toString());
                for (int j = 0; j < baoxiangongsichexianlist.size(); j++) {

                    if (baoxiangongsichexianlist.get(j).getLabel().equals(baoxiangongsiChexian.getText().toString())) {
                        sss.setId(baoxiangongsichexianlist.get(j).getId());

                    }
                }
                p.setId(xiugaiid);
                p.setInsuranceCompany(sss);
                p.setReportPhone(baoandianhuachexian.getText().toString());
                p.setInsuranceType("0");
                String str = gson.toJson(p);
                faqi = true;
                pagePresenter.subbaodanshuju(token, str, id, xiugaiid);
            } else {
                baodantuoguantankuang();
            }
        }

        dismissLoading();

    }

    public void baodantuoguantankuang() {
        new android.app.AlertDialog.Builder(this).setTitle("提示").setMessage("当前保单未完成，是否放弃本张保单并提交？")

                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent(BaoDanNew_Activity.this, BaoDanGeRenInfoActivity.class);
                        intent.putExtra("jiatingfshourushuzu", jiatingshourushuzu);
                        intent.putExtra("jiatingfshouruidshuzu", jiatingshourushuzuid);
                        intent.putExtra("jiatingfshouruidshuzuvalve", jiatingshourushuzuvalve);
                        intent.putExtra("beibaoxianrenjihe", (Serializable) beibaoxianrenjihe);
                        intent.putExtra("size", jiatingshourulist.size());
                        intent.putExtra("jiatingid", id);
                        startActivity(intent);
                        finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    public void initView() {
        CommonUtils.url_code = 0;

        titleImgBack.setImageResource(R.mipmap.fanhui);
        homeTvNum.setVisibility(View.VISIBLE);
        homeTvNum.setText("发起托管");
        titleImgBack.setOnClickListener(this);
        token = SystemConfigFactory.getInstance(BaoDanNew_Activity.this).getSystemConfig().getToken();
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(BaoDanNew_Activity.this).getSystemConfig().getColor()));

        TintManager.setTint(quesitonTj, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        quesitonTj.setOnClickListener(this);
        shifou = new String[2];
        shifou[0] = "寿险保单";
        shifou[1] = "车险保单";
        dianzibaodanshuzu = new String[2];
        dianzibaodanshuzu[0] = "否";
        dianzibaodanshuzu[1] = "是";
        baoxianqijianshuzu[0] = "终身";
        baoxianqijianshuzu[1] = "定期";
        //tankuangfanhui(shifou);
        Intent xinjian = getIntent();


        baodan = xinjian.getStringExtra("baodan");
        id = xinjian.getStringExtra("id");
        baodanshuliang = xinjian.getIntExtra("baodanshuliang", 0);
        beibaoxianrenjihe = (List<String>) xinjian.getSerializableExtra("beibaoxianrenjihe");
        if (baodan.equals("1")) {
            shouxianmokuai.setVisibility(View.VISIBLE);
            chexianmokuai.setVisibility(View.GONE);
            titleTopbar.setText("寿险保单");
            addshouxianchanpin();
        } else if (baodan.equals("2")) {
            shouxianmokuai.setVisibility(View.GONE);
            chexianmokuai.setVisibility(View.VISIBLE);
            titleTopbar.setText("车险保单");
        }


        pagePresenter.getBaoDanxuanXiang(token, "insuranceCompany");//baoxiangongsishuju

        pagePresenter.getBaoDanxuanXiang(token, "annualIncome");//jiatingshouru
        pagePresenter.getBaoDanxuanXiang(token, "protectionType");//baozhangleixing
        pagePresenter.getBaoDanxuanXiang(token, "belongsBank");//jiaofeiyinhang
        pagePresenter.getBaoDanxuanXiang(token, "insuranceCompany2");//baoxiangongsishuju

        baoxiangongsiShouxian.setOnClickListener(this);
        baoxiangongsiChexian.setOnClickListener(this);
        baodanshengxiaoshijian.setOnClickListener(this);
        chexianshengxiaoshijian.setOnClickListener(this);
        jiaofeikasuoshuyinhang.setOnClickListener(this);
        dianzibaodan.setOnClickListener(this);
        titleImgBack.setOnClickListener(this);
        homeTvNum.setOnClickListener(this);
        for (int i = 0; i < 31; i++) {
            if (i == 30) {
                jiaofeiqijianshuzu[i] = "终身";
            } else {
                jiaofeiqijianshuzu[i] = (i + 1) + "";
            }
        }

    }

    private void Subshuju() {
        CommonUtils.url_code = 0;
        //判断为空
        if (baodan.equals("1")) {

            if (!beibaoxianname.getText().toString().equals("")
                    && !toubaorenname.getText().toString().equals("")
                    && !baoxiangongsiShouxian.getText().toString().equals("")
                    && !baodanshengxiaoshijian.getText().toString().equals("")
                    && !jiaofeikasuoshuyinhang.getText().toString().equals("")
                    && !baoandianhua.getText().toString().equals("")
                    && !dianzibaodan.getText().toString().equals("")) {
                SubBaoDanShuJuBEan p = new SubBaoDanShuJuBEan();
                BelongsBank yinhang = new BelongsBank();

                beibaoxianrenjihe.add(beibaoxianname.getText().toString());

                InsuranceCompany sss = new InsuranceCompany();
                sss.setLabel(baoxiangongsiShouxian.getText().toString());
                sss.setValue(baoandianhua.getText().toString());
                for (int j = 0; j < baoxiangongsilist.size(); j++) {
//
                    if (baoxiangongsilist.get(j).getLabel().equals(baoxiangongsiShouxian.getText().toString())) {
                        sss.setId(baoxiangongsilist.get(j).getId());

                    }
                }
                p.setInsuranceCompany(sss);
                p.setInsuredName(beibaoxianname.getText().toString());
                p.setPolicyholderName(toubaorenname.getText().toString());


//


                List<policySheetTypeList> baozhang = new ArrayList<>();
                for (int i = 0; i < chanpinLin.getChildCount(); i++) {
                    View childAt = chanpinLin.getChildAt(i);

                    EditText chanpinname = childAt.findViewById(R.id.chanpinname);
                    EditText nianjiaobaofei = childAt.findViewById(R.id.nianjiaobaofei);
                    EditText baoe = childAt.findViewById(R.id.baoe);
                    TextView jiaofeinianxian = childAt.findViewById(R.id.jiaofeinianxian);
                    TextView baoxianqijian = childAt.findViewById(R.id.baoxianqijian);
                    TextView baoxiangleixingmorenwenzi = childAt.findViewById(R.id.baoxiangleixingmorenwenzi);


                    if (!chanpinname.getText().toString().equals("")
                            && !nianjiaobaofei.getText().toString().equals("")
                            && !baoe.getText().toString().equals("")
                            && !jiaofeinianxian.getText().toString().equals("")
                            && !baoxianqijian.getText().toString().equals("")
                            && !baoxiangleixingmorenwenzi.getText().toString().equals("")

                            ) {
                        policySheetTypeList baozhangleixingjiabaoe = new policySheetTypeList();
                        Dict dictduixiang = new Dict();
                        for(int j=0;j<baozhangleixinglist.size();j++){
                            if(baozhangleixinglist.get(j).getLabel().equals(baoxiangleixingmorenwenzi.getText().toString())){
                                dictduixiang.setId(baozhangleixinglist.get(j).getId());
                                dictduixiang.setLabel(baozhangleixinglist.get(j).getLabel());
                            }
                        }

                        baozhangleixingjiabaoe.setProtectionType(dictduixiang);
                        baozhangleixingjiabaoe.setInsuredAmount(baoe.getText().toString());
                        baozhangleixingjiabaoe.setPaymentPeriod(jiaofeinianxian.getText().toString());
                        baozhangleixingjiabaoe.setAnnualPremium(nianjiaobaofei.getText().toString());
                        baozhangleixingjiabaoe.setProductName(chanpinname.getText().toString());
                        baozhangleixingjiabaoe.setInsurePeriod(baoxianqijian.getText().toString());
                        baozhang.add(baozhangleixingjiabaoe);


                    } else {
                        ToastUtil.show(BaoDanNew_Activity.this, "您第" + (i + 1) + "组产品没有填写完整，请完善");
                    }

                }
                p.setPolicyEffectiveTime(baodanshengxiaoshijian.getText().toString());
                p.setpolicySheetTypeList(baozhang);
                yinhang.setLabel(jiaofeikasuoshuyinhang.getText().toString());
                for (int j = 0; j < suoshuyinhanglist.size(); j++) {
                    if (suoshuyinhanglist.get(j).getLabel().equals(jiaofeikasuoshuyinhang.getText().toString())) {
                        yinhang.setId(suoshuyinhanglist.get(j).getId());

                    }
                }
                p.setInsuranceType("1");
                p.setBelongsBank(yinhang);
                if (!yinhangkahao.getText().toString().equals("")) {
                    p.setBankNo(yinhangkahao.getText().toString());
                }
                p.setReportPhone(baoandianhua.getText().toString());
                if (dianzibaodan.getText().toString().equals("是")) {
                    p.setIsElectronic("1");
                } else {
                    p.setIsElectronic("0");
                }
                p.setId(xiugaiid);
                String str = gson.toJson(p);
                faqi = false;
                pagePresenter.subbaodanshuju(token, str, id, xiugaiid);
            } else {
                ToastUtil.show(BaoDanNew_Activity.this, "您的基本信息没有填写完整，请完善");
            }
        } else {
            if (!tianjiacheliang.getText().toString().equals("") && !chexianshengxiaoshijian.getText().toString().equals("")
                    && !baoxiangongsiChexian.getText().toString().equals("")
                    && !baoandianhuachexian.getText().toString().equals("")) {
                SubBaoDanShuJuBEan p = new SubBaoDanShuJuBEan();
//

                p.setNumberPlate(tianjiacheliang.getText().toString());

                p.setPolicyEffectiveTime(chexianshengxiaoshijian.getText().toString());
                InsuranceCompany sss = new InsuranceCompany();
                sss.setLabel(baoxiangongsiChexian.getText().toString());
                sss.setValue(baoandianhuachexian.getText().toString());
                for (int j = 0; j < baoxiangongsichexianlist.size(); j++) {

                    if (baoxiangongsichexianlist.get(j).getLabel().equals(baoxiangongsiChexian.getText().toString())) {
                        sss.setId(baoxiangongsichexianlist.get(j).getId());

                    }
                }
                p.setId(xiugaiid);
                p.setInsuranceCompany(sss);
                p.setReportPhone(baoandianhuachexian.getText().toString());
                p.setInsuranceType("0");
                String str = gson.toJson(p);
                Log.e("qubimana", "Subshuju: " + str);
                faqi = false;
                pagePresenter.subbaodanshuju(token, str, id, xiugaiid);
            } else {
                baodanshuju.clear();
                ToastUtil.show(BaoDanNew_Activity.this, "您的车险信息没有填写完整，请完善");
            }
        }

    }

    private void tankuangfanhui(final String[] shuzu) {

        AlertDialog.Builder builder = new AlertDialog.Builder(BaoDanNew_Activity.this);
        builder.setSingleChoiceItems(shuzu, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ListView lw = ((AlertDialog) dialog).getListView();
                // which表示点击的条目
                Object checkedItem = lw.getAdapter().getItem(which);
                Log.e("ninijijijij", "onClick: " + checkedItem);
                // 既然你没有cancel或者ok按钮，所以需要在点击item后使dialog消失
                dialog.dismiss();
                if (which == 0) {
                    showLoading();
                    baodanleixing = "1";
                    Subshuju();
                } else if (which == 1) {
                    showLoading();
                    baodanleixing = "2";
                    Subshuju();
                } else {
                    shouxianmokuai.setVisibility(View.GONE);
                    chexianmokuai.setVisibility(View.GONE);
                }


            }


        });
        AlertDialog dialog = builder.create();

        dialog.show();


    }

    private void baixianqijiantankuangfanhui(final String[] shuzu, final TextView v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(BaoDanNew_Activity.this);

        builder.setSingleChoiceItems(shuzu, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ListView lw = ((AlertDialog) dialog).getListView();
                // which表示点击的条目
                Object checkedItem = lw.getAdapter().getItem(which);
                // 既然你没有cancel或者ok按钮，所以需要在点击item后使dialog消失
                dialog.dismiss();
                if (which == 0) {
                    v.setText((String) checkedItem);
                } else {
                    tianbaoxiannianxian(v);
                }

            }


        });
        AlertDialog dialog = builder.create();
        dialog.show();
//        dialog.show();
//        builder.setCancelable(false);

    }

    private void tankuangfanhui(final String[] shuzu, final TextView v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(BaoDanNew_Activity.this);

        builder.setSingleChoiceItems(shuzu, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ListView lw = ((AlertDialog) dialog).getListView();
                // which表示点击的条目
                Object checkedItem = lw.getAdapter().getItem(which);
                // 既然你没有cancel或者ok按钮，所以需要在点击item后使dialog消失
                dialog.dismiss();
                v.setText((String) checkedItem);
            }


        });
        AlertDialog dialog = builder.create();

        dialog.show();
//        dialog.show();
//        builder.setCancelable(false);

    }

    //添加ViewItem
    private void addshouxianchanpin() {
        if (chanpinLin.getChildCount() == 0) {//如果一个都没有，就添加一个
            View hotelEvaluateView = View.inflate(this, R.layout.baozhangitem, null);
            LinearLayout btn_add = hotelEvaluateView.findViewById(R.id.tianjiaimg);
            TextView xianzhongname= hotelEvaluateView.findViewById(R.id.xianzhongname);
            xianzhongname.setText("主险名称：");
            btn_add.setVisibility(View.VISIBLE);
            btn_add.setTag("add");
            btn_add.setOnClickListener(this);
            chanpinLin.addView(hotelEvaluateView);
            sortHotelViewItem();
        } else {//如果有一个以上的Item,点击为添加的Item则添加
            View hotelEvaluateView = View.inflate(this, R.layout.baozhangitem, null);
            TextView xianzhongname= hotelEvaluateView.findViewById(R.id.xianzhongname);
            xianzhongname.setText("附加险名称：");
            chanpinLin.addView(hotelEvaluateView);

            sortHotelViewItem();
        }

    }

    /**
     * Item排序
     */
    private void sortHotelViewItem() {
        //获取LinearLayout里面所有的view
        for (int i = 0; i < chanpinLin.getChildCount(); i++) {
            final View childAt = chanpinLin.getChildAt(i);
            final ImageView btn_remove = childAt.findViewById(R.id.shanchuimg);
            final TextView baoxiangleixingmorenwenzi = childAt.findViewById(R.id.baoxiangleixingmorenwenzi);
            final TextView jiaofeiqijian = childAt.findViewById(R.id.jiaofeinianxian);
            final TextView baoxianqijian = childAt.findViewById(R.id.baoxianqijian);
            btn_remove.setTag("remove");//设置删除标记
            baoxianqijian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    baixianqijiantankuangfanhui(baoxianqijianshuzu, baoxianqijian);
                }
            });
            jiaofeiqijian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tankuangfanhui(jiaofeiqijianshuzu, jiaofeiqijian);
                }
            });
            baoxiangleixingmorenwenzi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tankuangfanhui(baozhangleixingshuzu, baoxiangleixingmorenwenzi);
                }
            });


            btn_remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //从LinearLayout容器中删除当前点击到的ViewItem
                    if (chanpinLin.getChildCount() > 1) {
                        chanpinLin.removeView(childAt);
                        sortHotelViewItem();
                    } else {
                        ToastUtil.show(BaoDanNew_Activity.this, "仅剩一组，无法删除");
                    }

                }
            });

            //如果是最后一个ViewItem，就设置为添加
            if (i == (chanpinLin.getChildCount() - 1)) {
                LinearLayout btn_add = childAt.findViewById(R.id.tianjiaimg);
                btn_add.setVisibility(View.VISIBLE);
                btn_add.setTag("add");
                btn_add.setOnClickListener(this);

            } else {
                LinearLayout btn_add = childAt.findViewById(R.id.tianjiaimg);
                btn_add.setVisibility(View.GONE);

            }
        }
    }

    /**
     * 日期选择
     */
    public void getDate(final TextView view) {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        final int[] date = {0, 0, 0};
        DateDialog birthDiolog = new DateDialog(this,
                new DateDialog.PriorityListener() {
                    @Override
                    public void refreshPriorityUI(String year, String month, String data) {
                        // TODO Auto-generated method stub

                        view.setText((year + "-" + month + "-" + data));

                    }
                }, date[0], date[1], date[2], width, height);
        Window window = birthDiolog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        birthDiolog.setCancelable(true);
        birthDiolog.show();
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }


    private void tankuangfanhui(final String[] shuzu, final TextView v, final TextView vv) {

        AlertDialog.Builder builder = new AlertDialog.Builder(BaoDanNew_Activity.this);
        builder.setSingleChoiceItems(shuzu, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ListView lw = ((AlertDialog) dialog).getListView();
                // which表示点击的条目
                Object checkedItem = lw.getAdapter().getItem(which);

                // 既然你没有cancel或者ok按钮，所以需要在点击item后使dialog消失
                dialog.dismiss();

                // 更新你的view
                if (checkedItem.equals("其他")) {
                    if (shuzu[0].equals(songsi[0])) {
                        ercengtankuang(v, vv, "1");
                    } else if (shuzu[0].equals(baoxiangongsichexian[0])) {
                        ercengtankuang(v, vv, "3");
                    } else {
                        ercengtankuang(v, vv, "2");
                    }


                } else {
                    if (shuzu[0].equals(songsi[0])) {
                        v.setText((String) checkedItem);
                        for (int i = 0; i < songsi.length; i++) {
                            if (v.getText().toString().equals(songsi[i])) {

                                vv.setText(baoandianhuashuzu[i]);
                            }
                        }
                    } else if (shuzu[0].equals(baoxiangongsichexian[0])) {
                        v.setText((String) checkedItem);
                        for (int i = 0; i < baoxiangongsichexian.length; i++) {
                            if (v.getText().toString().equals(baoxiangongsichexian[i])) {

                                vv.setText(baoandianhuachexianshuzu[i]);
                            }
                        }
                    } else {
                        v.setText((String) checkedItem);
                    }
                }
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();


    }

    public void tianbaoxiannianxian(final TextView views) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();

        View view = View.inflate(this, R.layout.tanchuzidingyikuang, null);
        // dialog.setView(view);// 将自定义的布局文件设置给dialog
        dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题

        final LinearLayout baoan = view.findViewById(R.id.baoan);
        final EditText baoxiangongsi = view.findViewById(R.id.baoxianhuoyinhangss);


        baoxiangongsi.setHint("请输入年龄或年限");
        baoan.setVisibility(View.GONE);

        Button btnOK = view.findViewById(R.id.btn_ok);
        Button btnCancel = view.findViewById(R.id.btn_cancel);

        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!baoxiangongsi.getText().toString().equals("")) {
                    views.setText(baoxiangongsi.getText().toString());
                    dialog.dismiss();// 隐藏dialog
                } else {
                    ToastUtil.show(BaoDanNew_Activity.this, "请填写年限或年龄");
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();// 隐藏dialog
            }
        });

        dialog.show();
    }

    public void ercengtankuang(final TextView views, final TextView viewss, String type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        typee = type;
        View view = View.inflate(this, R.layout.tanchuzidingyikuang, null);
        // dialog.setView(view);// 将自定义的布局文件设置给dialog
        dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题
        final TextView gongsihuoyinhang = view.findViewById(R.id.gongsihuoyinhang);
        final LinearLayout baoan = view.findViewById(R.id.baoan);
        final EditText baoxiangongsi = view.findViewById(R.id.baoxianhuoyinhangss);
        final EditText baoandianhua = view.findViewById(R.id.baoandianhua);
        if (typee.equals("1") || typee.equals("3")) {
            gongsihuoyinhang.setText("请输入公司名称：");
            baoan.setVisibility(View.VISIBLE);
        } else {
            gongsihuoyinhang.setText("请输入银行名称：");
            baoxiangongsi.setHint("请输入银行名称");
            baoan.setVisibility(View.GONE);
        }
        Button btnOK = view.findViewById(R.id.btn_ok);
        Button btnCancel = view.findViewById(R.id.btn_cancel);

        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                baoxiangongsiqita = baoxiangongsi.getText().toString();
                baoandianhuaqita = baoandianhua.getText().toString();
                if (gongsihuoyinhang.getText().equals("请输入公司名称：") && baoxiangongsiqita != null && !baoxiangongsiqita.equals("") && baoandianhuaqita != null && !baoandianhuaqita.equals("")) {
                    views.setText(baoxiangongsiqita);
                    if (typee.equals("1")) {
                        baoandianhuashuzu[baoxiangongsilist.size()] = baoandianhuaqita;
                        baoanxiabiao = baoxiangongsilist.size();
                        viewss.setText(baoandianhuaqita);
                    } else {
                        baoandianhuachexianshuzu[baoxiangongsichexianlist.size()] = baoandianhuaqita;
                        baoanxiabiao = baoxiangongsichexianlist.size();
                        viewss.setText(baoandianhuaqita);
                    }

                    dialog.dismiss();// 隐藏dialog
                } else if (gongsihuoyinhang.getText().equals("请输入银行名称：") && baoxiangongsiqita != null && !baoxiangongsiqita.equals("")) {
                    views.setText(baoxiangongsiqita);
                    dialog.dismiss();// 隐藏dialog
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();// 隐藏dialog
            }
        });

        dialog.show();
    }

    @Override
    public void getxuanxiangshuju(XuanXiangShuJuBean map) {
        if (map.getInsuranceCompany() != null) {

            baoxiangongsilist = map.getInsuranceCompany();

            songsi = new String[baoxiangongsilist.size() + 1];
            baoandianhuashuzu = new String[baoxiangongsilist.size() + 1];
            for (int i = 0; i < baoxiangongsilist.size(); i++) {
                songsi[i] = baoxiangongsilist.get(i).getLabel();
            }
            songsi[baoxiangongsilist.size()] = "其他";
            for (int i = 0; i < baoxiangongsilist.size(); i++) {
                baoandianhuashuzu[i] = baoxiangongsilist.get(i).getValue();
            }
        } else if (map.getProtectionType() != null) {
            baozhangleixinglist = map.getProtectionType();
            baozhangleixingshuzu = new String[baozhangleixinglist.size()];
            for (int i = 0; i < baozhangleixinglist.size(); i++) {
                baozhangleixingshuzu[i] = baozhangleixinglist.get(i).getLabel();
            }
        } else if (map.getBelongsBank() != null) {
            suoshuyinhanglist = map.getBelongsBank();
            suoshuyinhangshuzu = new String[suoshuyinhanglist.size() + 1];
            for (int i = 0; i < suoshuyinhanglist.size(); i++) {
                suoshuyinhangshuzu[i] = suoshuyinhanglist.get(i).getLabel();
            }
            suoshuyinhangshuzu[suoshuyinhanglist.size()] = "其他";
        } else if (map.getInsuranceCompany2() != null) {
            baoxiangongsichexianlist = map.getInsuranceCompany2();

            baoxiangongsichexian = new String[baoxiangongsichexianlist.size() + 1];
            baoandianhuachexianshuzu = new String[baoxiangongsichexianlist.size() + 1];

            for (int i = 0; i < baoxiangongsichexianlist.size(); i++) {
                baoxiangongsichexian[i] = baoxiangongsichexianlist.get(i).getLabel();
            }
            baoxiangongsichexian[baoxiangongsichexianlist.size()] = "其他";
            for (int i = 0; i < baoxiangongsichexianlist.size(); i++) {
                baoandianhuachexianshuzu[i] = baoxiangongsichexianlist.get(i).getValue();
            }
        } else {
            Intent in = getIntent();
            if (in.getStringExtra("bianji") != null && in.getStringExtra("bianji").equals("bianji")) {
                Log.e("sssssssssss", "getxuanxiangshuju: " + "2131");
                jiatingid = in.getStringExtra("id");
                xiugaiid = in.getStringExtra("xiugaiid");
                baodan = in.getStringExtra("baodan");
                pagePresenter.getyiyoushuju(token, jiatingid, xiugaiid);

            }
            jiatingshourulist = map.getAnnualIncome();
            jiatingshourushuzu = new String[jiatingshourulist.size()];
            jiatingshourushuzuid = new String[jiatingshourulist.size()];
            jiatingshourushuzuvalve = new String[jiatingshourulist.size()];
            for (int i = 0; i < jiatingshourulist.size(); i++) {
                jiatingshourushuzu[i] = jiatingshourulist.get(i).getLabel();
                jiatingshourushuzuid[i] = jiatingshourulist.get(i).getId();
                jiatingshourushuzuvalve[i] = jiatingshourulist.get(i).getValue();
            }
        }

    }

    @Override
    public void subbaodanshuju(BaoDanTiaoBean result) {
        if (!result.getFamilyId().equals("")) {
            dismissLoading();
            baodanshuliang++;
            if (faqi) {
                id = result.getFamilyId();

                Intent intent = new Intent(BaoDanNew_Activity.this, BaoDanGeRenInfoActivity.class);
                intent.putExtra("jiatingfshourushuzu", jiatingshourushuzu);
                intent.putExtra("jiatingfshouruidshuzu", jiatingshourushuzuid);
                intent.putExtra("jiatingfshouruidshuzuvalve", jiatingshourushuzuvalve);
                intent.putExtra("beibaoxianrenjihe", (Serializable) beibaoxianrenjihe);
                intent.putExtra("size", jiatingshourulist.size());
                intent.putExtra("jiatingid", id);
                startActivity(intent);
                finish();
            } else {
                baodanshuliang++;
                if (baodanleixing.equals("1")) {
                    beibaoxianrenjihe.add(beibaoxianname.getText().toString());
                    Intent intent = new Intent();
                    intent.setClass(BaoDanNew_Activity.this, BaoDanNew_Activity.class);
                    intent.putExtra("id", result.getFamilyId());
                    intent.putExtra("baodan", "1");
                    intent.putExtra("beibaoxianrenjihe", (Serializable) beibaoxianrenjihe);
                    intent.putExtra("baodanshuliang", baodanshuliang);
                    startActivity(intent);
                    finish();
                } else {
                    Intent intent = new Intent();
                    intent.setClass(BaoDanNew_Activity.this, BaoDanNew_Activity.class);
                    intent.putExtra("id", result.getFamilyId());
                    intent.putExtra("baodan", "2");
                    intent.putExtra("beibaoxianrenjihe", (Serializable) beibaoxianrenjihe);
                    intent.putExtra("baodanshuliang", baodanshuliang);
                    startActivity(intent);
                    finish();
                }

                // tankuangfanhui(shifou, result.getFamilyId());
            }


        }
    }

    @Override
    public void huoquyiyoushuju(SubBaoDanShuJuBEan result) {
        jiatingid = result.getFamilyId();
        xiugaiid = result.getId();
        baodan = result.getInsuranceType();
        if (baodan.equals("1")) {
            shouxianmokuai.setVisibility(View.VISIBLE);
            chexianmokuai.setVisibility(View.GONE);
            titleTopbar.setText("寿险保单");

        } else if (baodan.equals("2")) {
            shouxianmokuai.setVisibility(View.GONE);
            chexianmokuai.setVisibility(View.VISIBLE);
            titleTopbar.setText("车险保单");
        }
        if (baodan.equals("1")) {
            beibaoxianname.setText(result.getInsuredName());
            toubaorenname.setText(result.getPolicyholderName());
            baoxiangongsiShouxian.setText(result.getInsuranceCompany().getLabel());

            baodanshengxiaoshijian.setText(result.getPolicyEffectiveTime().substring(0, 10));
            jiaofeikasuoshuyinhang.setText(result.getBelongsBank().getLabel());
            if (result.getBankNo() != null) {
                yinhangkahao.setText(result.getBankNo());
            }
            baoandianhua.setText(result.getInsuranceCompany().getValue());
            if (result.getIsElectronic().equals("1")) {
                dianzibaodan.setText("是");
            } else {
                dianzibaodan.setText("否");
            }
            for (int i = 0; i < result.getpolicySheetTypeList().size(); i++) {
                if (i > 0) {
                    addshouxianchanpin();
                }
                final View childAt = chanpinLin.getChildAt(i);
                final EditText chanpinmingcheng = childAt.findViewById(R.id.chanpinname);
                final EditText nianjiaobaofei = childAt.findViewById(R.id.nianjiaobaofei);
                final EditText baoe = childAt.findViewById(R.id.baoe);
                final TextView baoxiangleixingmorenwenzi = childAt.findViewById(R.id.baoxiangleixingmorenwenzi);
                final TextView jiaofeiqijian = childAt.findViewById(R.id.jiaofeinianxian);
                final TextView baoxianqijian = childAt.findViewById(R.id.baoxianqijian);
                chanpinmingcheng.setText(result.getpolicySheetTypeList().get(i).getProductName());
                nianjiaobaofei.setText(result.getpolicySheetTypeList().get(i).getAnnualPremium());
                baoe.setText(result.getpolicySheetTypeList().get(i).getInsuredAmount());
                baoxiangleixingmorenwenzi.setText(result.getpolicySheetTypeList().get(i).getProtectionType().getLabel());
                jiaofeiqijian.setText(result.getpolicySheetTypeList().get(i).getPaymentPeriod());
                baoxianqijian.setText(result.getpolicySheetTypeList().get(i).getInsurePeriod());
            }


        } else {


            tianjiacheliang.setText(result.getNumberPlate());
            chexianshengxiaoshijian.setText(result.getPolicyEffectiveTime());
            baoxiangongsiChexian.setText(result.getInsuranceCompany().getLabel());
            baoandianhua.setText(result.getInsuranceCompany().getValue());
        }


    }

    @Override
    public void erroy() {

    }

    @Override
    public void erry(String result) {
        dismissLoading();

        Toast.makeText(BaoDanNew_Activity.this, result, Toast.LENGTH_SHORT).show();
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(BaoDanNew_Activity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(BaoDanNew_Activity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }


}
